package com.example.ktapplication.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.ktapplication.R
import com.example.ktapplication.bean.Media
import com.example.ktapplication.databinding.RvVideoItemLayoutBinding
import com.example.ktapplication.utils.LogHelper
import xyz.doikki.videocontroller.StandardVideoController
import xyz.doikki.videocontroller.component.PrepareView
import xyz.doikki.videocontroller.component.VodControlView
import xyz.doikki.videoplayer.player.BaseVideoView
import xyz.doikki.videoplayer.player.VideoView

class VideoListAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val medias by lazy {
        mutableListOf<Media>()
    }

    var itemCallBack: OnItemClickListener? = null

    private var videoView: VideoView? = null
    private var curPlayUrl = ""

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.rv_video_item_layout, parent, false)
        return MusicViewHolder(view)
    }

    override fun getItemCount(): Int {
        return medias.size
    }

    @SuppressLint("NotifyDataSetChanged")
    fun addData(data: MutableList<Media>) {
        medias.clear()
        data.sortBy { it.title }
        medias.addAll(data)
        notifyDataSetChanged()
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MusicViewHolder) {
            val media = medias[position]
            //重置播放器
            holder.binding.vvCloudPanel.apply {
                resetVideo(media.playUrl) {
                    holder.binding.ivPlay.isVisible = it
                }
            }
            //点击播放
            holder.binding.ivPlay.setOnClickListener {
                videoView?.resetVideo(curPlayUrl)
                holder.binding.vvCloudPanel.playVideo(media.playUrl)
                itemCallBack?.onItemClick(media)
            }
            //视频来源
            holder.binding.tvTitle.text = media.title
            holder.binding.tvUser.text = media.user
        }
    }

    inner class MusicViewHolder(item: View) : RecyclerView.ViewHolder(item) {
        val binding by lazy {
            RvVideoItemLayoutBinding.bind(item)
        }
    }

    interface OnItemClickListener {
        fun onItemClick(media: Media)
    }

    fun onBackPressed(): Boolean {
        return videoView?.onBackPressed() ?: false
    }

    private fun VideoView.playVideo(playUrl: String) {
        resetVideo(playUrl)
        setUrl(playUrl)
        start()
        LogHelper.e("---ext playVideo $playUrl")
        curPlayUrl = playUrl
        videoView = this
    }

    private fun VideoView.resetVideo(playUrl: String, action: (Boolean) -> Unit = {}) {
        pause()
        release()
        addVideoConfig(playUrl, action)
    }

    private fun VideoView.addVideoConfig(playUrl: String, action: (Boolean) -> Unit = {}) {
        StandardVideoController(context).apply {
            //准备播放界面
            val prepareView = PrepareView(context).run {
                findViewById<ImageView>(xyz.doikki.videocontroller.R.id.start_play).apply {
                    val lp = layoutParams
                    lp.width = 0
                    lp.height = 0
                    layoutParams = lp
                }
                val thumb = findViewById<ImageView>(xyz.doikki.videocontroller.R.id.thumb) //封面图
                Glide.with(this).load(playUrl).into(thumb)
                this
            }
            addControlComponent(prepareView)
            //控制区域
            addControlComponent(VodControlView(context))
            addOnStateChangeListener(object : BaseVideoView.OnStateChangeListener {
                override fun onPlayerStateChanged(playerState: Int) {
                }

                override fun onPlayStateChanged(playState: Int) {
                    when (playState) {
                        VideoView.STATE_IDLE,
                        VideoView.STATE_PLAYBACK_COMPLETED,
                        VideoView.STATE_START_ABORT,
                        VideoView.STATE_ERROR ->
                            action.invoke(true)

                        else ->
                            action.invoke(false)
                    }
                }
            })
            setVideoController(this)
        }
    }

    fun pausePlayer() {
        videoView?.resetVideo(curPlayUrl)
    }

}